package com.sunny.util;

import org.apache.commons.validator.routines.EmailValidator;

import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Sunny on 2016/4/15.
 */
public class StringUtil {

    private static final EmailValidator emailValidator = EmailValidator.getInstance();

    public StringUtil() {
    }

    public static String urlencoder(String str) {
        return urlencoder(str, "utf-8");
    }

    public static String urlencoder(String str, String charsetName) {
        if(str != null && str != "") {
            try {
                return URLEncoder.encode(str, charsetName);
            } catch (UnsupportedEncodingException var3) {
                var3.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    public static String urldecoder(String str) {
        return urldecoder(str, "UTF-8");
    }

    public static String urldecoder(String str, String charsetName) {
        if(str != null && str != "") {
            try {
                return URLDecoder.decode(str, charsetName);
            } catch (UnsupportedEncodingException var3) {
                var3.printStackTrace();
                return "";
            }
        } else {
            return "";
        }
    }

    public static String delHtml(String content) {
        return RegexUtil.deleteHTML(content);
    }

    public static String toScriptStr(String str) {
        if(isEmpty(str)) {
            return "";
        } else {
            str = str.replace("\\", "\\\\").replace("\n", "\\n").replace("\"", "\\\"").replaceAll("\t", " ").replaceAll("  ", " ").replace("\r", "");
            return str;
        }
    }

    public static String showShort(String content, int length, String mark) {
        if(content == null) {
            return null;
        } else if(content.getBytes().length <= length) {
            return content;
        } else {
            byte[] s = content.getBytes();
            int flag = 0;

            for(int d = 0; d < length; ++d) {
                if(s[d] < 0) {
                    ++flag;
                }
            }

            if(flag % 2 != 0) {
                --length;
            }

            byte[] var6 = new byte[length];
            System.arraycopy(s, 0, var6, 0, length);
            return new String(var6) + mark;
        }
    }

    public static String showShort(String content, int length) {
        return showShort(content, length, "..");
    }

    public static String toHtml(String str) {
        String temp = str.replaceAll(" ", "&nbsp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br />").replaceAll("\r", "");
        return temp;
    }

    public static String dtoHtml(String str) {
        if(isEmpty(str)) {
            return "";
        } else {
            String temp = delHtml(str);
            temp = toHtml(temp);
            return temp;
        }
    }

    public static String dtoSafeHtml(String str) {
        if(isEmpty(str)) {
            return "";
        } else {
            String temp = delHtml(str);
            temp = toSafeHtml(temp);
            return temp;
        }
    }

    public static String getMD5(String str) {
        if(str != null && str.length() != 0) {
            StringBuffer hexString = new StringBuffer();

            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(str.getBytes());
                byte[] hash = md.digest();

                for(int i = 0; i < hash.length; ++i) {
                    if((255 & hash[i]) < 16) {
                        hexString.append("0" + Integer.toHexString(255 & hash[i]));
                    } else {
                        hexString.append(Integer.toHexString(255 & hash[i]));
                    }
                }
            } catch (NoSuchAlgorithmException var5) {
                ;
            }

            return hexString.toString();
        } else {
            return "";
        }
    }

    public static String get16Md5(String plainText) {
        try {
            MessageDigest e = MessageDigest.getInstance("MD5");
            e.update(plainText.getBytes());
            byte[] b = e.digest();
            StringBuffer buf = new StringBuffer("");

            for(int offset = 0; offset < b.length; ++offset) {
                int i = b[offset];
                if(i < 0) {
                    i += 256;
                }

                if(i < 16) {
                    buf.append("0");
                }

                buf.append(Integer.toHexString(i));
            }

            return buf.toString().substring(8, 24);
        } catch (NoSuchAlgorithmException var6) {
            var6.printStackTrace();
            return "";
        }
    }

    public static int[] splitArr(String sid, String type) {
        if(isEmpty(sid)) {
            return null;
        } else {
            String[] arr = sid.split(type);
            int[] ids = new int[arr.length];
            int i = 0;
            String[] var8 = arr;
            int var7 = arr.length;

            for(int var6 = 0; var6 < var7; ++var6) {
                String s = var8[var6];
                ids[i] = NumberUtil.parseInt(s);
                ++i;
            }

            return ids;
        }
    }

    public static String joinArr(Object[] arr, String type) {
        if(arr == null) {
            return "";
        } else {
            StringBuilder tmp = new StringBuilder();
            int i = 0;
            Object[] var7 = arr;
            int var6 = arr.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                Object o = var7[var5];
                String s = String.valueOf(o);
                if(i > 0) {
                    tmp.append(type);
                }

                tmp.append(s);
                ++i;
            }

            return tmp.toString();
        }
    }

    public static String joinArrforInt(int[] arr, String type, int out) {
        if(arr == null) {
            return "";
        } else {
            StringBuilder tmp = new StringBuilder();
            int i = 0;
            int[] var8 = arr;
            int var7 = arr.length;

            for(int var6 = 0; var6 < var7; ++var6) {
                int o = var8[var6];
                if(o != out) {
                    if(i > 0) {
                        tmp.append(type);
                    }

                    tmp.append(String.valueOf(o));
                    ++i;
                }
            }

            return tmp.toString();
        }
    }

    public static String joinArrforSerializable(Serializable[] arr, String type) {
        if(arr == null) {
            return "";
        } else {
            StringBuilder tmp = new StringBuilder();
            int i = 0;
            Serializable[] var7 = arr;
            int var6 = arr.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                Serializable o = var7[var5];
                if(o != null) {
                    if(i > 0) {
                        tmp.append(type);
                    }

                    tmp.append(String.valueOf(o));
                    ++i;
                }
            }

            return tmp.toString();
        }
    }
/*

    public static String joinArrforSstr(String[] arr, String type) {
        if(arr == null) {
            return "";
        } else {
            StringBuilder tmp = new StringBuilder();
            int i = 0;
            String[] var7 = arr;
            int var6 = arr.length;

            for(int var5 = 0; var5 < var6; ++var5) {
                String o = var7[var5];
                int j = Integer.valueOf(SecurityUtil.decrypt(o)).intValue();
                if(j > 0) {
                    if(i > 0) {
                        tmp.append(type);
                    }

                    tmp.append(String.valueOf(j));
                    ++i;
                }
            }

            return tmp.toString();
        }
    }
*/

    public static boolean findArr(String index, String strarr, String type) {
        if(isEmpty(strarr)) {
            return false;
        } else {
            String[] l = strarr.split(type);

            for(int i = 0; i < l.length; ++i) {
                if(l[i].equals(index)) {
                    return true;
                }
            }

            return false;
        }
    }

    public static int indexArr(String index, String strarr, String type) {
        if(isEmpty(strarr)) {
            return -1;
        } else {
            String[] l = strarr.split(type);

            for(int i = 0; i < l.length; ++i) {
                if(l[i].equals(index)) {
                    return i;
                }
            }

            return -1;
        }
    }

    public static String delArr(String index, String strarr, String type) {
        if(isEmpty(strarr)) {
            return "";
        } else {
            String[] l = strarr.split(type);
            StringBuilder tmp = new StringBuilder();

            for(int i = 0; i < l.length; ++i) {
                if(!l[i].equals(index) && l[i].length() > 0) {
                    if(tmp.length() > 0) {
                        tmp.append(type);
                    }

                    tmp.append(l[i]);
                }
            }

            return tmp.toString();
        }
    }

    public static String joinString(String[] str, String type) {
        StringBuilder s = new StringBuilder();
        int i = 0;
        String[] var7 = str;
        int var6 = str.length;

        for(int var5 = 0; var5 < var6; ++var5) {
            String value = var7[var5];
            if(i > 0 && !isEmpty(type)) {
                s.append(type);
            }

            s.append(value);
            ++i;
        }

        return s.toString();
    }

    public static String setSafeUrl(String url) {
        if(url.indexOf("?") >= 0) {
            url = url + "&";
        } else {
            url = url + "?";
        }

        return url;
    }

    public static String toSafeStr(String str) {
        if(str == null) {
            return "";
        } else {
            str = str.replaceAll("\"", "＂").replaceAll("\'", "＇").replaceAll("\t", " ").replaceAll("  ", " ").replaceAll("\r", "").replaceAll("\n", "\\n");
            return str;
        }
    }

    public static String delNullStr(String str) {
        return str == null?"":str;
    }

    public static String utf2gbk(String str) {
        String tmp = "";

        try {
            tmp = new String(str.getBytes("utf-8"), "gbk");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
        }

        return tmp;
    }

    public static String gbk2utf(String str) {
        String tmp = "";

        try {
            tmp = new String(str.getBytes("gbk"), "utf-8");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
        }

        return tmp;
    }

    public static String decodeQuotedPrintable(String str) {
        byte ESCAPE_CHAR = 61;
        if(str == null) {
            return null;
        } else {
            byte[] bytes = str.getBytes();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            for(int e = 0; e < bytes.length; ++e) {
                byte b = bytes[e];
                if(b == ESCAPE_CHAR) {
                    try {
                        ++e;
                        int e1 = Character.digit((char)bytes[e], 16);
                        ++e;
                        int l = Character.digit((char)bytes[e], 16);
                        if(e1 == -1 || l == -1) {
                            return "";
                        }

                        buffer.write((char)((e1 << 4) + l));
                    } catch (ArrayIndexOutOfBoundsException var9) {
                        return "";
                    }
                } else {
                    buffer.write(b);
                }
            }

            bytes = buffer.toByteArray();
            str = "";

            try {
                str = new String(bytes, "UTF-8");
            } catch (UnsupportedEncodingException var8) {
                var8.printStackTrace();
            }

            return str;
        }
    }

    public static boolean isEmpty(String str) {
        return str == null?true:str.length() == 0;
    }

    public static boolean isNotEmpty(String str) {
        return str != null?str.length() != 0:false;
    }

    public static boolean isEquals(String str1, String str2) {
        return str1 == null?str2 == null:str1.equals(str2);
    }

    public static boolean isEqualsIn(String str1, String[] sourceArray) {
        if(!isEmpty(str1) && sourceArray != null && sourceArray.length != 0) {
            String[] var5 = sourceArray;
            int var4 = sourceArray.length;

            for(int var3 = 0; var3 < var4; ++var3) {
                String source = var5[var3];
                if(isEquals(str1, source)) {
                    return true;
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static boolean endsWithIgnoreCase(String beChecked, String str) {
        return beChecked != null && str != null?beChecked.toUpperCase().endsWith(str.toUpperCase()):false;
    }

    public static boolean isEmail(String str) {
        return emailValidator.isValid(str);
    }

    public static char[] bytesToChars(byte[] bytes, int offset, int length) {
        String s = new String(bytes, offset, length);
        char[] chars = s.toCharArray();
        return chars;
    }

    public static String findStr(String source, String start, String end) {
        if(source == null) {
            return null;
        } else {
            boolean i = false;
            int i1;
            if(!isEmpty(start)) {
                i1 = source.indexOf(start);
                if(i1 < 0) {
                    return null;
                }

                source = source.substring(i1 + start.length());
            }

            if(!isEmpty(end)) {
                i1 = source.indexOf(end);
                if(i1 < 0) {
                    return null;
                }

                source = source.substring(0, i1);
            }

            return source;
        }
    }

    public static String getIndexString(int index, int length) {
        String sDatesBefor;
        for(sDatesBefor = String.valueOf(index); sDatesBefor.length() < length; sDatesBefor = "0" + sDatesBefor) {
            ;
        }

        return sDatesBefor;
    }

    public static String getIndexPath(int index, int length) {
        StringBuffer tmp = new StringBuffer();
        String idstr = getIndexString(index, length);

        for(int i = 0; i < idstr.length(); i += 2) {
            tmp.append("/" + idstr.substring(i, i + 2));
        }

        return tmp.toString();
    }

    public static String toSafeHtml(String content) {
        content = content.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
        content = RegexUtil.toLink(content);
        return content.replaceAll("  ", "&nbsp;").replaceAll("\n", "<br>");
    }

    public static String getEmailExt(String email) {
        return email.substring(email.indexOf("@") + 1);
    }

    public static String getEmailName(String email) {
        return email.substring(0, email.indexOf("@"));
    }

    public static String getFrist2LowerCase(String str) {
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static int[] stringArray2int(String[] arr) {
        int[] tmparr = new int[arr.length];

        for(int i = 0; i < arr.length; ++i) {
            tmparr[i] = NumberUtil.parseInt(arr[i]);
        }

        return tmparr;
    }

    public static String trim(String str) {
        return str == null?null:str.trim();
    }

    public static void main(String[] args) {
        System.out.println(isUrl((String)null));
        System.out.println(isUrl(""));
        System.out.println(isUrl("aa"));
        System.out.println(isUrl("http://aa"));
        System.out.println(isUrl("http://aa.com"));
    }

    public static boolean isUrl(String str) {
        try {
            new URL(str);
            return true;
        } catch (MalformedURLException var2) {
            return false;
        }
    }

    public static String htmlEncode(String source) {
        if(source == null) {
            return "";
        } else {
            String html = "";
            StringBuffer buffer = new StringBuffer();

            for(int i = 0; i < source.length(); ++i) {
                char c = source.charAt(i);
                switch(c) {
                    case '\n':
                    case '\r':
                        break;
                    case '\"':
                        buffer.append("&quot;");
                        break;
                    case '&':
                        buffer.append("&amp;");
                        break;
                    case '<':
                        buffer.append("&lt;");
                        break;
                    case '>':
                        buffer.append("&gt;");
                        break;
                    case '`':
                        buffer.append("\'");
                        break;
                    default:
                        buffer.append(c);
                }
            }

            html = buffer.toString();
            return html;
        }
    }
}
